/**
 * 商品详情页面js
 */
var bsizes = "";
// 男鞋尺码，
var gsizes = "";
// 女鞋尺码
var num1 = 0;
//第一个评论列表评论的个数
var num2 = 0;
//第2个评论列表评论的个数
var num3 = 0;
//第3个评论列表评论的个数
var num4 = 0;
//第4个评论列表评论的个数
var nowopencomment = 1;

/*--男女复选框改变时触发*/
function manChange(box, isman) {
    var isboy = document.getElementById("isboy");
    var isgirl = document.getElementById("isgirl");
    if (!isboy.checked && !isgirl.checked) {
        alert('适应人群至少选择一个');
        if (isman == '0') {
            isgirl.checked = true;
        } else {
            isboy.checked = true;
        }
        return;
    }
    if (box.checked) {
        if (isman == '0') {
            $(".girlclass").each(function() {
                $(this).css("display", "");
            });
        } else {
            $(".boyclass").each(function() {
                $(this).css("display", "");
            });
        }

    } else {
        if (isman == '0') {
            $(".girlclass").each(function() {
                $(this).css("display", "none");
                var eles = document.getElementsByName("girlmm");
                for (var i = 0; i < eles.length; i++) {
                    eles[i].value = "";
                }
            });
        } else {
            $(".boyclass").each(function() {
                $(this).css("display", "none");
                var eles = document.getElementsByName("boymm");
                for (var i = 0; i < eles.length; i++) {
                    eles[i].value = "";
                }
            });
        }
    }

}

/*-删除一行-*/
function removeSize(obj) {
    var o = $(obj);
    o.parents("tr").remove();
}

/* 增加鞋码 */
function addSize() {
    var isboy = document.getElementById("isboy");
    var isgirl = document.getElementById("isgirl");
    if (isboy.checked && isgirl.checked) {
        $("#sizeTable").append('<tr><td style="width: 150px;"><input  type="text" name="size" style="width: 80px;"/>码</td><td style="width: 180px;" class="boyclass">男<input type="text" name="boymm" style="width: 80px;" />MM </td><td style="width: 180px;" class="girlclass">女<input type="text" name="girlmm" style="width: 80px;" />MM </td><td><button type="button"style="width: 50px; background-color: #ffffff;" onclick="removeSize(this)"class="am-btn am-btn-secondary"><font color="#0e90d2">-</font></button></td></tr>');
    } else if (isboy.checked && !isgirl.checked) {
        $("#sizeTable").append('<tr><td style="width: 150px;"><input  type="text" name="size" style="width: 80px;"/>码</td><td style="width: 180px;" class="boyclass">男<input type="text" name="boymm" style="width: 80px;" />MM </td><td style="width: 180px;display:none" class="girlclass">女<input type="text" name="girlmm" style="width: 80px;" />MM </td><td><button type="button"style="width: 50px; background-color: #ffffff;" onclick="removeSize(this)"class="am-btn am-btn-secondary"><font color="#0e90d2">-</font></button></td></tr>');
    } else if (!isboy.checked && isgirl.checked) {
        $("#sizeTable").append('<tr><td style="width: 150px;"><input  type="text" name="size" style="width: 80px;"/>码</td><td style="width: 180px;display:none" class="boyclass">男<input type="text" name="boymm" style="width: 80px;" />MM </td><td style="width: 180px;" class="girlclass">女<input type="text" name="girlmm" style="width: 80px;" />MM </td><td><button type="button"style="width: 50px; background-color: #ffffff;" onclick="removeSize(this)"class="am-btn am-btn-secondary"><font color="#0e90d2">-</font></button></td></tr>');
    }

}

/* 打开已选择的评论列表 */
function openSelected() {
    var first = $(".comment" + nowopencomment + " .first");
    var second = $(".comment" + nowopencomment + " .second");
    var third = $(".comment" + nowopencomment + " .third");
    var selComment = document.getElementById("selectComment");
    //var selComment=$("#selectComment");
    var url = '';
    selComment.innerHTML="";
    if (first.val() != null && first.val() != "") {
        url += '<p style="font-size: 10px;">' + first.val() + '<a  style="font-size: 12px;cursor:pointer;" onclick="delcomment(1)">&nbsp;删除</a></p>';
    }
    if (second.val() != null && second.val() != "") {
        url += '<p style="font-size: 10px;">' + second.val() + '<a  style="font-size: 12px;cursor:pointer;"  onclick="delcomment(2)">&nbsp;删除</a></p>';
    }
    if (third.val() != null && third.val() != "") {
        url += '<p style="font-size: 10px;">' + third.val() + '<a  style="font-size: 12px;cursor:pointer;"  onclick="delcomment(3)">&nbsp;删除</a></p>';
    }
    selComment.innerHTML+=url;
    var $modal = $('#your-modal2');
    var commentlist = document.getElementsByName("commentname");
    for (var i = 0; i < commentlist.length; i++) {
        commentlist[i].checked = false;
    }
    $modal.modal();
}

/*-删除评论*/
function delcomment(index) {
    var first = $(".comment" + nowopencomment + " .first");
    var second = $(".comment" + nowopencomment + " .second");
    var third = $(".comment" + nowopencomment + " .third");
    var selComment = document.getElementById("selectComment");
    var url = '';
    selComment.innerHTML = "";
    if (index == '1') {
        first.val("");
        num1 -= 1;
    } else if (index == '2') {
        second.val("");
        num1 -= 1;
    } else {
        third.val("");
        num1 -= 1;
    }

    if (first.val() != null && first.val() != "") {
        url += '<p style="font-size: 10px;">' + first.val() + '<a  style="font-size: 12px;cursor:pointer;" onclick="delcomment(1)">&nbsp;删除</a></p>';
    }
    if (second.val() != null && second.val() != "") {
        url += '<p style="font-size: 10px;">' + second.val() + '<a  style="font-size: 12px;cursor:pointer;"  onclick="delcomment(2)">&nbsp;删除</a></p>';
    }
    if (third.val() != null && third.val() != "") {
        url += '<p style="font-size: 10px;">' + third.val() + '<a  style="font-size: 12px;cursor:pointer;"  onclick="delcomment(3)">&nbsp;删除</a></p>';
    }
    selComment.innerHTML += url;

}

/* 评论选择改变时触发 */
function commentChange(thisbox, content, name, time) {
    var first = $(".comment" + nowopencomment + " .first");
    var second = $(".comment" + nowopencomment + " .second");
    var third = $(".comment" + nowopencomment + " .third");
    if (thisbox.checked) {
        var num = null;
        switch (nowopencomment) {
            case 1:
                num = num1;
                break;
            case 2:
                num = num2;
                break;
            case 3:
                num = num3;
                break;
            case 4:
                num = num4;
                break;

            default:
                break;
        }
        if (num < 3) {
            if (first.val() == null || first.val() == "") {
                first.val(content + "," + name + "," + time);
                switch (nowopencomment) {
                    case 1:
                        num1 += 1;
                        break;
                    case 2:
                        num2 += 1;
                        break;
                    case 3:
                        num3 += 1;
                        break;
                    case 4:
                        num4 += 1;
                        break;
                    default:
                        break;
                }
                return;
            }
            if (second.val() == null || second.val() == "") {
                second.val(content + "," + name + "," + time);
                switch (nowopencomment) {
                    case 1:
                        num1 += 1;
                        break;
                    case 2:
                        num2 += 1;
                        break;
                    case 3:
                        num3 += 1;
                        break;
                    case 4:
                        num4 += 1;
                        break;
                    default:
                        break;
                }
                return;
            }
            if (third.val() == null || third.val() == "") {
                third.val(content + "," + name + "," + time);
                switch (nowopencomment) {
                    case 1:
                        num1 += 1;
                        break;
                    case 2:
                        num2 += 1;
                        break;
                    case 3:
                        num3 += 1;
                        break;
                    case 4:
                        num4 += 1;
                        break;
                    default:
                        break;
                }
                return;
            }
        } else {
            thisbox.checked = false;
            alert("最多只能选择三天评论！");
        }

    }
}

/* 打开选择评论的窗口 */
function opendialog(index) {
    nowopencomment = index;
    var $modal = $('#your-modal');
    var commentlist = document.getElementsByName("commentname");
    for (var i = 0; i < commentlist.length; i++) {
        commentlist[i].checked = false;
    };
    $modal.modal();
}

/* 关闭选择评论的窗口 */
function closedialog(index) {
    var $modal = $('#your-modal');
    $modal.modal('close');
}

function submit() {
    /*
     * window.open('../buyer/index','newwindow','height=410,width=650,top=150,left=300,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,
     * status=no')
     */
    var numid = $("#numid").val();
    var isman = 0;
    var sizes = "";
    var title = $('#name').val();
    var boy = document.getElementById("isboy");
    var girl = document.getElementById("isgirl");

    /*获取出四个评论标签的评论列表*/
    var first1 = $(".comment1 .first").val();
    var second1 = $(".comment1 .second").val();
    var third1 = $(".comment1 .third").val();
    var comment1 = first1 + ";" + second1 + ";" + third1;

    var first2 = $(".comment2 .first").val();
    var second2 = $(".comment2 .second").val();
    var third2 = $(".comment2 .third").val();
    var comment2 = first2 + ";" + second2 + ";" + third2;

    var first3 = $(".comment3 .first").val();
    var second3 = $(".comment3 .second").val();
    var third3 = $(".comment3 .third").val();
    var comment3 = first3 + ";" + second3 + ";" + third3;

    var first4 = $(".comment4 .first").val();
    var second4 = $(".comment4 .second").val();
    var third4 = $(".comment4 .third").val();
    var comment4 = first4 + ";" + second4 + ";" + third4;

    var sport = $('input[name="docInlineRadio"]:checked ').val();
    var stout = $("#stout").val();
    var normal = $("#normal").val();
    var lean = $("#lean").val();
    if (sport == undefined) {
        alert('请选择鞋子类型');
        return;
    }

    if (boy.checked) {
        var eles = document.getElementsByName("boymm");
        for (var i = 0; i < eles.length; i++) {
            if (eles[i].value == "") {
                alert("请将男鞋尺码填写完整！");
                return;
            } else {
                bsizes += eles[i].value + ",";
            }

        }
    }

    if (girl.checked) {
        var eles = document.getElementsByName("girlmm");
        for (var i = 0; i < eles.length; i++) {
            if (eles[i].value == "") {
                alert("请将女鞋尺码填写完整！");
                return;
            } else {
                gsizes += eles[i].value + ",";
            }
        }
    }

    var eles = document.getElementsByName("size");
    for (var i = 0; i < eles.length; i++) {
        sizes += eles[i].value + ",";
    }

    if (gsizes == "") {
        isman = "1";
    } else if (bsizes == "") {
        isman = "0";
    } else if (gsizes != "" && bsizes != "") {
        isman = "2";
    }
    $.post("../selleradmin/insertProduct", {
        comment1 : comment1,
        comment2 : comment2,
        comment3 : comment3,
        comment4 : comment4,
        numid : numid,
        title : title,
        gsizes : gsizes,
        bsizes : bsizes,
        isman : isman,
        sizes : sizes,
        sport : sport,
        stout : stout,
        normal : normal,
        lean : lean
    }, function(data) {
        alert(data);
       window.location.href="../selleradmin/toProductList?pageNo=1";
    });
}

/*-根据title获取模板尺码*/
function getTempletSize(e) {
    var title = $(e).val();
    $.post("../selleradmin/getSizeBytitle", {
        title : title
    }, function(data) {
        var isboy = document.getElementById("isboy");
        var isgirl = document.getElementById("isgirl");
        if (data != null) {
            $("#sizeTable").html('');
            for (var i = 0; i < data.length; i++) {
                if (isboy.checked && isgirl.checked) {
                    $("#sizeTable").append('<tr><td style="width: 150px;"><input  type="text" name="size" style="width: 80px;" value="' + data[i].french_size + '"/>码</td><td style="width: 180px;" class="boyclass">男<input type="text" name="boymm" style="width: 80px;" value="' + data[i].size + '"/>MM </td><td style="width: 180px;" class="girlclass">女<input type="text" name="girlmm" style="width: 80px;" value="' + data[i].size + '"/>MM </td><td><button type="button"style="width: 50px; background-color: #ffffff;" onclick="removeSize(this)"class="am-btn am-btn-secondary"><font color="#0e90d2">-</font></button></td></tr>');
                } else if (isboy.checked && !isgirl.checked) {
                    $("#sizeTable").append('<tr><td style="width: 150px;"><input  type="text" name="size" style="width: 80px;" value="' + data[i].french_size + '"/>码</td><td style="width: 180px;" class="boyclass">男<input type="text" name="boymm" style="width: 80px;" value="' + data[i].size + '"/>MM </td><td style="width: 180px;display:none" class="girlclass">女<input type="text" name="girlmm" style="width: 80px;" value="' + data[i].size + '"/>MM </td><td><button type="button"style="width: 50px; background-color: #ffffff;" onclick="removeSize(this)"class="am-btn am-btn-secondary"><font color="#0e90d2">-</font></button></td></tr>');
                } else if (!isboy.checked && isgirl.checked) {
                    $("#sizeTable").append('<tr><td style="width: 150px;"><input  type="text" name="size" style="width: 80px;" value="' + data[i].french_size + '"/>码</td><td style="width: 180px;display:none" class="boyclass">男<input type="text" name="boymm" style="width: 80px;" value="' + data[i].size + '"/>MM </td><td style="width: 180px;" class="girlclass">女<input type="text" name="girlmm" style="width: 80px;" value="' + data[i].size + '"/>MM </td><td><button type="button"style="width: 50px; background-color: #ffffff;" onclick="removeSize(this)"class="am-btn am-btn-secondary"><font color="#0e90d2">-</font></button></td></tr>');
                }
            }
        }

    });

}
function toTemplet() {
    window.location.href="../selleradmin/templet";
}
function opencomment() {
	$("#addcomment").toggle("slow");
}

